package cn.iocoder.yudao.module.system.service.workstroerecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.controller.admin.workappendage.vo.WorkAppendageSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.workorder.vo.WorkOrderRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.workappendage.WorkAppendageDO;
import cn.iocoder.yudao.module.system.dal.dataobject.workorder.WorkOrderDO;
import cn.iocoder.yudao.module.system.dal.dataobject.workstore.WorkStoreDO;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.dal.mysql.workorder.WorkOrderMapper;
import cn.iocoder.yudao.module.system.dal.mysql.workstore.WorkStoreMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workstroerecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workstroerecord.WorkStroeRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workstroerecord.WorkStroeRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 入库托数记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WorkStroeRecordServiceImpl implements WorkStroeRecordService {

    @Resource
    private WorkStroeRecordMapper workStroeRecordMapper;
    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private WorkStoreMapper workStoreMapper;
    @Resource
    private WorkOrderMapper workOrderMapper;

    @Override
    public Long createWorkStroeRecord(WorkStroeRecordSaveReqVO createReqVO) {
        // 插入
        WorkStroeRecordDO workStroeRecord = BeanUtils.toBean(createReqVO, WorkStroeRecordDO.class);
        workStroeRecordMapper.insert(workStroeRecord);
        // 返回
        return workStroeRecord.getId();
    }

    @Override
    public void updateWorkStroeRecord(WorkStroeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkStroeRecordExists(updateReqVO.getId());
        // 更新
        WorkStroeRecordDO updateObj = BeanUtils.toBean(updateReqVO, WorkStroeRecordDO.class);
        workStroeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkStroeRecord(Long id) {
        // 校验存在
        validateWorkStroeRecordExists(id);
        // 删除
        workStroeRecordMapper.deleteById(id);
    }

    @Override
        public void deleteWorkStroeRecordListByIds(List<Long> ids) {
        // 校验存在
        validateWorkStroeRecordExists(ids);
        // 删除
        workStroeRecordMapper.deleteByIds(ids);
        }

    private void validateWorkStroeRecordExists(List<Long> ids) {
        List<WorkStroeRecordDO> list = workStroeRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_STROE_RECORD_NOT_EXISTS);
        }
    }

    private void validateWorkStroeRecordExists(Long id) {
        if (workStroeRecordMapper.selectById(id) == null) {
            throw exception(WORK_STROE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public WorkStroeRecordDO getWorkStroeRecord(Long id) {
        return workStroeRecordMapper.selectById(id);
    }

    @Override
    public PageResult<WorkStroeRecordRespVO> getWorkStroeRecordPage(WorkStroeRecordPageReqVO pageReqVO) {
        PageResult<WorkStroeRecordDO> arr = workStroeRecordMapper.selectPage(pageReqVO);
        PageResult<WorkStroeRecordRespVO> data = BeanUtils.toBean(arr, WorkStroeRecordRespVO.class);
        List<String> updateIds = new ArrayList<>();
        List<Integer> workIds = new ArrayList<>();
        List<Integer> storeIds = new ArrayList<>();
        for (WorkStroeRecordRespVO item:data.getList()) {
            updateIds.add(item.getUpdater());
            workIds.add(item.getWorkId());
            storeIds.add(item.getStoreId());
        }
        List<AdminUserDO> adminUserDOS = new ArrayList<>();
        List<WorkStoreDO> workStoreDOS = new ArrayList<>();
        List<WorkOrderDO> workOrderDOS = new ArrayList<>();
        if(updateIds.size()>0){
           adminUserDOS = adminUserMapper.selectList(new QueryWrapper<AdminUserDO>().in("id",updateIds));
        }
        if(storeIds.size()>0){
            workStoreDOS = workStoreMapper.selectList(new QueryWrapper<WorkStoreDO>().in("id",storeIds));
        }
        if(workIds.size()>0){
            workOrderDOS = workOrderMapper.selectList(new QueryWrapper<WorkOrderDO>().in("id",workIds));
        }

        for (WorkStroeRecordRespVO item:data.getList()) {
            for (AdminUserDO userDO:adminUserDOS) {
                if(userDO.getId().equals(Long.valueOf(item.getUpdater()))){
                    item.setUpdaterName(userDO.getNickname());break;
                }
            }
            for (WorkStoreDO storeDO:workStoreDOS) {
                if(storeDO.getId().equals(item.getStoreId())){
                    item.setBatchNumber(storeDO.getBatchNumber());break;
                }
            }
            for (WorkOrderDO workOrderDO:workOrderDOS) {
                if(workOrderDO.getId().equals(Long.valueOf(item.getWorkId()))){
                    item.setCustomerName(workOrderDO.getCustomerName());
                    item.setProductName(workOrderDO.getProductName());
                    item.setSpecification(workOrderDO.getSpecification());break;
                }
            }
        }
        return data;
    }

}
